package com.weight.common.generate.base;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.weight.common.enums.ResultEnum;
import com.weight.common.exception.CSException;
import com.weight.common.generate.base.JavaFile.Method;

/**
 * 
 * @author sdzs
 *
 */
public class CommonUtil {
	static Collection<String> descriptionList=null;//方法描述注释
	static List<String> annotations = null;//方法 标签 注解
	static StringBuilder modifier = new StringBuilder("public");//方法开放程度  public
	static StringBuilder resultType=null;//方法返回类型
	static StringBuilder methodName=null;// 方法名
	static StringBuilder args=null;//方法 入参 
	static List<String> exceptions = null;//抛出异常
	static StringBuilder methodBody = null;//方法体 具体实现业务
	static List<String>argLists=null;//方法 入参別名
	/**
	 * Mapper/Service   接口实现
	 * @param methodLists
	 * @param model
	 * @param tableCnName
	 */
	public static void insertMethod(List<Method> methodLists,String model,String tableCnName,boolean hasBody,boolean isService,List<String> fieldENList) {
		String cnName=tableCnName.substring(0, tableCnName.length()-1);
		final StringBuilder beanName=getModelClass(model);
		StringBuilder mapperName=new StringBuilder(model).append("Mapper.");
		annotations = new LinkedList<String>();
		if(hasBody)annotations.add("Override");
		Method method =null;
	
		if(model.endsWith("Info")||model.endsWith("Log")||model.endsWith("Norm")) {
	//查询 条件查询
			//返回类型
			resultType = new StringBuilder("List<").append(beanName).append(">");
			//方法名
			methodName = new StringBuilder("find").append(beanName).append("ByT");
			//入参
			args = new StringBuilder().append("Map map");
			//入参名
			argLists=new ArrayList<String>();	
			argLists.add("map");
			//方法注释描述
			descriptionList=descriptionModel(new StringBuilder("查询").append(cnName).append("信息--条件查询"),argLists,true);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append("return ").append(mapperName).append(methodName).append("(map);")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
	//查询分页----Service层独有
		if(isService) {
			//返回类型
			resultType = new StringBuilder("List<").append(beanName).append(">");
			//方法名
			methodName = new StringBuilder("find").append(beanName).append("ByPage");
			//入参
			args = new StringBuilder("int currentPage,int pageSize,Map map");
			//入参名
			argLists=new ArrayList<String>();	
			argLists.add("currentPage");
			argLists.add("pageSize");
			argLists.add("map");
			//方法注释描述
			descriptionList=descriptionModel(new StringBuilder("查询").append(cnName).append("信息--条件，分页查询"),argLists,true);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder("PageHelper.startPage(currentPage, pageSize);"),
						new StringBuilder().append("List<").append(beanName).append("> ")
							.append(model).append("List = ").append(mapperName).append("find")
							.append(beanName).append("ByT(map);"),
						new StringBuilder().append("return ").append(model).append("List;")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
		}
	//查询全部
			//返回类型
			resultType = new StringBuilder("List<").append(beanName).append(">");
			//方法名
			methodName = new StringBuilder("findAll");
			//入参
			args =new StringBuilder();
			//入参名
			argLists=new ArrayList<String>();	
			//方法注释描述
			descriptionList=descriptionModel(new StringBuilder("查询").append(cnName).append("信息--查询全部"),argLists,true);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append("return ").append(mapperName).append("findAll();")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);	
	//ID查询
			resultType=beanName;
			methodName = new StringBuilder("findBy").append(beanName).append("Id");
			args = new StringBuilder("Map map");
			argLists=new ArrayList<String>();	
			argLists.add("map");
			descriptionList=descriptionModel(new StringBuilder("ID查询").append(cnName).append("信息"),argLists,true);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(beanName).append(" ").append(model).append("=").append(mapperName).append(methodName).append("(map);"),
						new StringBuilder().append("CommonUtils.validateExistObj(").append(model).append(",map);"),
						new StringBuilder().append("return ").append(model).append(";")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
	//ID查询-批量
			resultType=new StringBuilder("List");
			methodName = new StringBuilder("findBy").append(beanName).append("IdList");
			args = new StringBuilder("List ").append(model).append("IdList");
			argLists=new ArrayList<String>();	
			argLists.add(new StringBuilder(model).append("IdList").toString());
			
			descriptionList=descriptionModel(new StringBuilder("ID查询").append(cnName).append("信息-批量"),argLists,true);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append("List idList = ").append(mapperName).append(methodName).append("(")
										   .append(model).append("IdList").append( ");"),
						new StringBuilder().append("CommonUtils.validateExistList(idList,").append(model).append("IdList);"),
						new StringBuilder().append("return idList;")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
	//添加
			resultType = new StringBuilder("void");
			methodName = new StringBuilder("insert").append(beanName);
			args = new StringBuilder().append(beanName).append(" ").append(model);
			argLists=new ArrayList<String>();	
			argLists.add(model);
			descriptionList=descriptionModel(new StringBuilder("添加").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(").append(model).append(");")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
	//批量添加
			resultType = new StringBuilder("void");
			methodName = new StringBuilder("insert").append(beanName).append("Batch");
			args = new StringBuilder("List<").append(beanName).append("> ").append(model).append("List");
			argLists=new ArrayList<String>();	
			argLists.add(new StringBuilder(model).append("List").toString());
			descriptionList=descriptionModel(new StringBuilder("批量添加").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(").append(model).append("List);")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
	//修改
			resultType = new StringBuilder("void");
			methodName = new StringBuilder("update").append(beanName);
			args = new StringBuilder().append(beanName).append(" ").append(model);
			argLists=new ArrayList<String>();	
			argLists.add(model);
			descriptionList=descriptionModel(new StringBuilder("修改").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(").append(model).append(");")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
	//修改Limt
			resultType = new StringBuilder("void");
			methodName = new StringBuilder("update").append(beanName).append("Limt");
			args = new StringBuilder().append(beanName).append(" ").append(model);
			argLists=new ArrayList<String>();	
			argLists.add(model);
			descriptionList=descriptionModel(new StringBuilder("修改").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(").append(model).append(");")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
	//批量修改
			resultType = new StringBuilder("void");
			methodName = new StringBuilder("update").append(beanName).append("Batch");
			args = new StringBuilder("List<").append(beanName).append("> ").append(model).append("List");
			argLists=new ArrayList<String>();	
			argLists.add(new StringBuilder(model).append("List").toString());
			descriptionList=descriptionModel(new StringBuilder("批量修改").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(").append(model).append("List);")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
	//物理删除	
			resultType=new StringBuilder("void");
			methodName = new StringBuilder("deleteBy").append(beanName).append("Id");
			args = new StringBuilder("Map map");
			argLists=new ArrayList<String>();	
			argLists.add("map");
			descriptionList=descriptionModel(new StringBuilder("物理删除").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(map);")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);		
	//批量物理删除	
			resultType=new StringBuilder("void");
			methodName = new StringBuilder("deleteBy").append(beanName).append("IdBatch");
			args = new StringBuilder("List ").append(model).append("IdList");
			argLists=new ArrayList<String>();	
			argLists.add(new StringBuilder(model).append("IdList").toString());
			descriptionList=descriptionModel(new StringBuilder("批量物理删除").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(").append(model).append("IdList);")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);	
		
		}
		
		
		
		if(model.endsWith("Info")||model.endsWith("Log")) {
	//状态删除
			resultType = new StringBuilder("void");
			methodName = new StringBuilder("upDelBy").append(beanName).append("Id");
			args = new StringBuilder("Map map");
			argLists=new ArrayList<String>();	
			argLists.add("map");
			descriptionList=descriptionModel(new StringBuilder("状态删除").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(map);")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
	//批量状态删除
			resultType = new StringBuilder("void");
			methodName = new StringBuilder("upDelBy").append(beanName).append("IdBatch");
			args = new StringBuilder("Map<String, Object> map");
			argLists=new ArrayList<String>();	
			argLists.add(new StringBuilder(model).append("List").toString());
			descriptionList=descriptionModel(new StringBuilder("批量状态删除").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(map);")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
		}
		
		
		
		
		
		if(model.endsWith("Ral")) {
			String ralId=getModelClass(fieldENList.get(0)).toString();
			String ralId2=getModelClass(fieldENList.get(1)).toString();
		//ID查询对象ralId
			resultType=new StringBuilder("List<").append(beanName).append(">");
			methodName = new StringBuilder("findBy").append(ralId);
			args = new StringBuilder("Map map");
			argLists=new ArrayList<String>();	
			argLists.add("map");
			descriptionList=descriptionModel(new StringBuilder("ID查询").append(cnName).append("对象信息"),argLists,true);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder("List<").append(beanName).append("> ").append(model).append("List=").append(mapperName).append(methodName).append("(map);"),
						new StringBuilder().append("return ").append(model).append("List;")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
		
		//ID查询对象ralId2
			resultType=new StringBuilder("List<").append(beanName).append(">");
			methodName = new StringBuilder("findBy").append(ralId2);
			args = new StringBuilder("Map map");
			argLists=new ArrayList<String>();	
			argLists.add("map");
			descriptionList=descriptionModel(new StringBuilder("ID查询").append(cnName).append("对象信息"),argLists,true);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder("List<").append(beanName).append("> ").append(model).append("List=").append(mapperName).append(methodName).append("(map);"),
						new StringBuilder().append("return ").append(model).append("List;")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
			
		//ID查询ralId2
			resultType=new StringBuilder("List<String>");
			methodName = new StringBuilder("find").append(ralId2).append("By").append(ralId);
			args = new StringBuilder("Map map");
			argLists=new ArrayList<String>();	
			argLists.add("map");
			descriptionList=descriptionModel(new StringBuilder("ID查询").append(cnName).append(ralId2).append("信息"),argLists,true);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder("List<String> ").append(ralId2).append("List=").append(mapperName).append(methodName).append("(map);"),
						new StringBuilder().append("return ").append(ralId2).append("List;")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
			
		//ID查询	ralId
			resultType=new StringBuilder("List<String>");
			methodName = new StringBuilder("find").append(ralId).append("By").append(ralId2);
			args = new StringBuilder("Map map");
			argLists=new ArrayList<String>();	
			argLists.add("map");
			descriptionList=descriptionModel(new StringBuilder("ID查询").append(cnName).append(ralId).append("信息"),argLists,true);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder("List<String> ").append(ralId).append("List=").append(mapperName).append(methodName).append("(map);"),
						new StringBuilder().append("return ").append(ralId).append("List;")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
			
		//批量添加
			resultType = new StringBuilder("void");
			methodName = new StringBuilder("insert").append(beanName).append("Batch");
			args = new StringBuilder("List<").append(beanName).append("> ").append(model).append("List");
			argLists=new ArrayList<String>();	
			argLists.add(new StringBuilder(model).append("List").toString());
			descriptionList=descriptionModel(new StringBuilder("批量添加").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(").append(model).append("List);")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
			
		//物理删除	
			resultType=new StringBuilder("void");
			methodName = new StringBuilder("deleteBy").append(ralId);
			args = new StringBuilder("Map map");
			argLists=new ArrayList<String>();	
			argLists.add("map");
			descriptionList=descriptionModel(new StringBuilder("物理删除").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(map);")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
			
		//物理删除	
			resultType=new StringBuilder("void");
			methodName = new StringBuilder("deleteBy").append(ralId2);
			args = new StringBuilder("Map map");
			argLists=new ArrayList<String>();	
			argLists.add("map");
			descriptionList=descriptionModel(new StringBuilder("物理删除").append(cnName),argLists,false);
			if(hasBody) {
				methodBody = buildMethod(new StringBuilder[] {
						new StringBuilder().append(mapperName).append(methodName).append("(map);")
				});
			}
			method = new Method(descriptionList,annotations, modifier, resultType, methodName, args, exceptions, methodBody);
			methodLists.add(method);
		}
		
		
		
		
		
		
		
		
	}
	/**
	 * 接口/方法注释描述
	 * @param beanName
	 * @param argLists
	 * @param hasReturn
	 * @return
	 */
	public static Collection<String> descriptionModel(StringBuilder beanName,List argLists,boolean hasReturn) {
		Collection<String> descriptionList=new LinkedList<String>();
		descriptionList.add("/**");
		if(beanName!=null)descriptionList.add(new StringBuffer(" * ").append(beanName).toString());
		if(argLists!=null&&argLists.size()>0) {
			for(int i=0;i<argLists.size();i++) {
				descriptionList.add(new StringBuffer(" * @param ").append(argLists.get(i)).toString());
			}
		}
		if(hasReturn)descriptionList.add(" * @return");
		descriptionList.add(" */");
		return descriptionList;
	}
	/**
	 * 首字母大写
	 * @param model
	 * @return
	 */
	public static StringBuilder getModelClass(String model) {
	    char[] ch = model.toCharArray();  
	    if (ch[0] >= 'a' && ch[0] <= 'z') {  
	        ch[0] = (char) (ch[0] - 32);  
	    }  
		StringBuilder modelClass = new StringBuilder(new String(ch));
		return modelClass;
	}
//	public static StringBuilder getImportName(String packagePath,String model) {
//		StringBuilder importName = new StringBuilder();
//		importName.append(packagePath);
//		importName.append(".PO.");
//		importName.append(getModelClass(model));
//		return importName;
//	}
//	public static StringBuilder getImportName(String packagePath,String and,String name) {
//		StringBuilder importName = new StringBuilder();
//		importName.append(packagePath);
//		importName.append(".");
//		importName.append(and);
//		importName.append(".");
//		importName.append(name);
//		return importName;
//	}
	
	private static StringBuilder buildMethod(StringBuilder[] lines) {
		StringBuilder method = new StringBuilder();
		if(lines != null) {
			for (StringBuilder line : lines) {
				method.append("\t\t")
					.append(line)
					.append("\n");
			}
		}
		return method;
	}
}
